package com.foreverwin.jzymes.production.custcmpln.controller;

import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.FTPUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.custcmpln.domain.CustCmplnLog;
import com.foreverwin.jzymes.production.custcmpln.enums.CustCmplnStatusEnum;
import com.foreverwin.jzymes.production.custcmpln.mapper.CustCmplnLogMapper;
import com.foreverwin.jzymes.production.custcmpln.service.ICustCmplnAdiviceService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/customer")
public class CustomerController extends BaseController {
    @Autowired
    ICustCmplnAdiviceService custCmplnAdiviceService;
    @Autowired
    CustCmplnLogMapper custCmplnLogMapper;
    @Autowired
    FTPUtil ftpUtil;

    @RequestMapping("/insertCustCmpln")
    public AjaxResult insertCustCmpln(@RequestBody CustCmplnLog custCmplnLog){
        try{
            custCmplnLog.setSite(CommonMethods.getSite());
            custCmplnLog.setCreateUserBo( CommonMethods.getUserBo() );
            custCmplnLog.setCreatedDateTime( new Date());


            custCmplnAdiviceService.insertCustCmpln( custCmplnLog );

            return AjaxResult.returnSuccess().put("value",custCmplnLog);

        }catch (BusinessException e){
            return  AjaxResult.returnError( e.getMessage() );
        }

    }

    @RequestMapping("/updateCustCmpln")
    public AjaxResult updateCustCmpln(@RequestBody CustCmplnLog custCmplnLog){
        try{
            custCmplnAdiviceService.updateCustCmpln( custCmplnLog );
            return AjaxResult.returnSuccess().put("value",custCmplnLog);

        }catch (BusinessException e){
            return  AjaxResult.returnError( e.getMessage() );
        }


    }

    @RequestMapping("/deleteCustCmpln")
    public AjaxResult deleteCustCmpln(@RequestBody List<String> cust){
        try{

            for( int i= 0;i< cust.size();i++){
               CustCmplnLog custCmplnLog =  custCmplnLogMapper.selectCustCmplnLogById( cust.get( i ) );
               Assert.notNull( custCmplnLog , I18nUtil.getI18nText("production.custcmpln.datanotExsited"));
               /**结案状态不可以进行操作*/
               Assert.isTrue( !custCmplnLog.getStatus().equals( CustCmplnStatusEnum.CLOSED.value() ) , I18nUtil.getI18nText("production.custcmpln.StatusIsC") );

               String annex = custCmplnLog.getAnnex();
               String annexPath = custCmplnLog.getAnnexPath();
               Assert.notNull( annexPath , I18nUtil.getI18nText("production.custcmpln.AnnexPathNotExsited") );
               Assert.notNull( annex , I18nUtil.getI18nText("production.custcmpln.AnnexNotExsited") );
               String remotePath =  "CustCmplnLog"+File.separator+"Annex"+File.separator;

               if ( !StringUtils.isEmpty( annex )){
                  //ftpUtil.deleteFile( remotePath );
                   ftpUtil.deleteFile1( remotePath + annexPath );
               }
               custCmplnLogMapper.deleteCustCmplnLogById( cust.get( i ) );
            }
            return AjaxResult.returnSuccess();

        }catch ( Exception e ){
            return  AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping("/searchCustCmpln")
    public AjaxResult searchCustCmpln(@RequestParam Map<String,Object> jsonMap){
        try{
            jsonMap.put("site",CommonMethods.getSite());
            List<CustCmplnLog> custCmplnLogs = custCmplnAdiviceService.searchCustCmpln( jsonMap );
            return AjaxResult.returnSuccess().put("value", custCmplnLogs );
        }catch (BusinessException e){
            return  AjaxResult.returnError( e.getMessage() );
        }

    }
    @RequestMapping(value = "/uploadAnnex")
    public AjaxResult uploadAnnex(@RequestParam String machineType,@RequestParam String customer ,@RequestParam String cmplnType ,@RequestParam String approach ,@RequestParam MultipartFile file){
        try{
             String name = custCmplnAdiviceService.uploadAnnex( machineType ,customer,cmplnType,approach,file );
            return AjaxResult.returnSuccess().put("value",name);
        }catch (BusinessException e){
            return  AjaxResult.returnError( e.getMessage() );
        }

    }

    @RequestMapping(value = "/downloadAnnex")
    public void downloadAnnex( String paramJson, HttpServletResponse response, HttpServletRequest request  ){
        try {
            Map<String,Object> paramMap = ObjectMapperUtil.readValue( paramJson, HashMap.class );

            String annexPath = (String) paramMap.get("annexPath");
            Assert.notNull( annexPath , I18nUtil.getI18nText("production.custcmpln.AnnexPathNotExsited") );
            String annex = (String) paramMap.get("annex");
            Assert.notNull( annex , I18nUtil.getI18nText("production.custcmpln.AnnexNotExsited") );

            String romotePath = "CustCmplnLog"+File.separator+"Annex";
            String last = annexPath.substring( annexPath.lastIndexOf(".")+1 );
            String frist = annex.substring(0,annex.indexOf("."));

            String downloadDir = ftpUtil.getDownloadDir();
            File tempFile = new File( downloadDir + File.separator + romotePath );
            tempFile.mkdirs();

            ftpUtil.download( romotePath+File.separator+annexPath , new File( tempFile.getPath()+ File.separator+frist+"."+last ) );

            File file =  new File( tempFile.getPath()+ File.separator+frist+"."+last );

            fileDownload( file , true ,response ,request );

            FileUtils.deleteDirectory( tempFile.getParentFile() );

        }  catch (Exception e) {
           throw BusinessException.build(e.getMessage());
        }
    }

}
